package com.itheima.controller;

import com.itheima.annotation.Log;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/depts")
@Slf4j
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result getList() {
        log.info("查询部门列表");
        // 调用service层代码查询部门列表数据
        List<Dept> depts = deptService.getList();

        // 返回结果
        return Result.success(depts);
    }

    @Log
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) throws Exception {
        log.info("删除部门，要删除的部门ID：{}", id);

        deptService.deleteById(id);

        return Result.success();
    }

    @Log
    @PostMapping
    public Result insert(@RequestBody Dept dept) {
        log.info("新增部门，要新增部门的名称：{}", dept.getName());

        deptService.insert(dept);

        return Result.success();
    }
}

















